<template>
  <div class="goods-tabs">
    <nav>
      <a
        href="javascript:;"
        v-for="item in types"
        :key="item.id"
        :class="{ active: curName === item.type }"
        @click="toggle(item.type)"
      >{{item.name}}</a>
    </nav>
    <!-- 这个位置显示对应的组件 GoodsDetail 或者 GoodsComment -->
    <!--
      component
      is
      componentId  组件名称
     -->
    <!-- <good-comment v-if="curName === 'comment'"/>
    <good-detail v-else/> -->
    <component :is="componentId"></component>
  </div>
</template>
<script>
import { ref } from 'vue'
import GoodDetail from './good-detail'
import GoodComment from './good-comment'
export default {
  name: 'GoodsTabs',
  components: {
    GoodDetail,
    GoodComment
  },
  setup () {
    // 当前激活项
    const curName = ref('detail')
    // 所有可选项
    const types = [
      {
        id: 1,
        type: 'detail',
        name: '商品详情'
      },
      {
        id: 2,
        type: 'comment',
        name: '商品评价'
      }
    ]
    // 切换回调函数
    function toggle (type) {
      curName.value = type
      // type  detail  comment
      // 点击a切换componentId的值
      componentId.value = `good-${type}`
    }

    const componentId = ref('good-detail')
    return {
      curName,
      toggle,
      types,
      componentId
    }
  }
}
</script>
<style scoped lang="less">
.goods-tabs {
  min-height: 600px;
  background: #fff;
  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;
      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
      &:first-child {
        border-right: 1px solid #f5f5f5;
      }
      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @xtxColor;
        }
      }
    }
  }
}
</style>
